package sd.czw.com.mmgapp.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.model.MyLoginUserInfoCallback;
import sd.czw.com.mmgapp.model.UserInfo;
import sd.czw.com.mmgapp.model.loginandsign.LoginNormalCallback;
import sd.czw.com.mmgapp.model.loginandsign.LoginNormalModel;
import sd.czw.com.mmgapp.utils.SpUtils;

/**
 * Created by czw
 * desc 描述：
 * on 2017/7/13.
 */
public class RegistActivity extends AppCompatActivity {

    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rl_right)
    RelativeLayout rlRight;
    @Bind(R.id.edt_yqm)
    EditText edtYqm;
    @Bind(R.id.edt_yzm)
    EditText edtYzm;
    @Bind(R.id.edt_zh)
    EditText edtZh;
    @Bind(R.id.edt_mm)
    EditText edtMm;
    @Bind(R.id.edt_pmm)
    EditText edtPmm;
    @Bind(R.id.regist)
    Button regist;
    private String phonenumber;
    private Map<String,String> params = new HashMap<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        ButterKnife.bind(this);

        tvTitle.setText("会员注册");
        rlRight.setVisibility(View.GONE);
        rlBack.setVisibility(View.VISIBLE);

        phonenumber = getIntent().getStringExtra("phonenumber");
        edtZh.addTextChangedListener(watcher);
        edtYzm.addTextChangedListener(watcher);
        edtMm.addTextChangedListener(watcher);
        edtPmm.addTextChangedListener(watcher);

        regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String trim = edtYqm.getText().toString().trim();//邀请码
                String s = edtYzm.getText().toString().trim();//验证码
                String s1 = edtZh.getText().toString().trim();//账号
                String s2 = edtMm.getText().toString().trim();//密码
                String s3 = edtPmm.getText().toString().trim();//密码
                if (phonenumber != null){
                    params.put("mobile_phone",phonenumber);
                }
                if (trim != null && !("").equals(trim)){
                    params.put("invitecode",trim);
                }
                if(s != null && !("".equals(s))){
                    params.put("code",s);
                }
                if (s1 != null && !("".equals(s1))){
                    params.put("username",s1);
                }
                if (s2 != null && !("".equals(s2))){
                    params.put("password",s2);
                }
                params.put("time",Contants.Time);
                params.put("sign",Contants.Sign);
                params.put("act","register");
                if (s2.equals(s3)) {
                    OkHttpUtils.post().url(Contants.NMLOGIN)
//                            .addParams("username", s1)
//                            .addParams("mobile_phone", phonenumber)
//                            .addParams("act", "register")
//                            .addParams("password", s2)
//                            .addParams("code", s)
//                            .addParams("time", Contants.Time)
//                            .addParams("sign", Contants.Sign)
                            .params(params)
                            .build().execute(new LoginNormalCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Toast.makeText(RegistActivity.this, "错误："+e.getMessage(), Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onResponse(LoginNormalModel response, int id) {
                            if (response.getErrcode() == 1) {
                                Intent intent = new Intent(RegistActivity.this,LoginActivity.class);
                                startActivity(intent);
                                Toast.makeText(RegistActivity.this, "注册成功，可以去登录啦", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(RegistActivity.this, "" + response.getErrmsg(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }else {
                    Toast.makeText(RegistActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                }
            }
        });
        RegistActivity.this.startActivity(new Intent(RegistActivity.this, LoginActivity.class));
    }

    @OnClick({R.id.rl_back})
    void onclick(View view){
        switch (view.getId()){
            case R.id.rl_back:
                onBackPressed();
                break;
//            case R.id.regist:
//                //startActivity(new Intent(RegistActivity.this, LoginActivity.class));
//                String s = edtYzm.getText().toString().trim();
//                String s1 = edtZh.getText().toString().trim();
//                String s2 = edtMm.getText().toString().trim();
//                String s3 = edtPmm.getText().toString().trim();
//                if (s2.equals(s3)) {
//                    OkHttpUtils.post().url(Contants.NMLOGIN)
//                            .addParams("username", s1)
//                            .addParams("mobile_phone", phonenumber)
//                            .addParams("act", "register")
//                            .addParams("password", s2)
//                            .addParams("code", s)
//                            .addParams("time", Contants.Time)
//                            .addParams("sign", Contants.Sign)
//                            .build().execute(new LoginNormalCallback() {
//                        @Override
//                        public void onError(Call call, Exception e, int id) {
//
//                        }
//
//                        @Override
//                        public void onResponse(LoginNormalModel response, int id) {
//                            if (response.getErrcode() == 1) {
//                                Toast.makeText(RegistActivity.this, "注册成功，可以去登录啦", Toast.LENGTH_SHORT).show();
//                                RegistActivity.this.startActivity(new Intent(RegistActivity.this, LoginActivity.class));
//                            } else {
//                                Toast.makeText(RegistActivity.this, "" + response.getErrmsg(), Toast.LENGTH_SHORT).show();
//                            }
//                        }
//                    });
//                }else {
//                    Toast.makeText(RegistActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
//                }
//                break;
        }
    }


    TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            String s = edtMm.getText().toString();
            String s1 = edtPmm.getText().toString();
            String s2 = edtYzm.getText().toString();
            String s3 = edtZh.getText().toString();
            if (s != null && s1 != null && s2 != null && s3 != null && !("".equals(s)) && !("".equals(s1)) && !("".equals(s2)) && !("".equals(s3))){
                regist.setBackground(getDrawable(R.drawable.login_checkbtn));
            }else {
                regist.setBackground(getDrawable(R.drawable.login_btn));
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
}
